/**
 * 课题js
 */

$(function () {
	initialPage();
	getGrid();
});

function initialPage() {
	$(window).resize(function() {
		$('#dataGrid').bootstrapTable('resetView', {height: $(window).height()-56});
	});
}

//更多按钮操作
function  btnAction(_this,code,id){
    // $(_this).find("option[value='']").attr("selected", "selected");
    switch(code){
        case '':
             return;
             break;
        case 'edit':
            vm.edit(id);
            break;
        case 'shangjia':
            vm.setActive(1,id);
            break;
        case 'xiajia':
            vm.setActive(0,id);
            break;
        case 'endLive':
            vm.endLive(id);
            break;
        case 'delete':
            vm.remove(id);
            break;
        // case 'ppt':
        //     vm.ppt(classRoomId);
        //     break;
        default:break;
    }
    // $(_this).find("option[value='']").attr("selected", "selected");
    // setTimeout(function(){
    //     $(_this).find("option[value='']").attr("selected", "selected");
    //     console.info($(_this).find("option:first").val());
    // },500);
}

function getGrid() {
	$('#dataGrid').bootstrapTableEx({
		url: '../../srblive/classroom/list?_' + $.now(),
		height: $(window).height()-56,
		queryParams: function(params){
			params.keyword = vm.keyword;
			return params;
		},
        singleSelect: true,
		columns: [
			{checkbox: true},
            {field : "title", title : "课题标题", width : "150px"},
            // {field : "subtitle", title : "副标题", width : "100px"},

            {field : "type", title : "课题类型", width : "80px",
                formatter: function (value, row, index) {
                    return Utils.commonFmt(value,row,index,conts.classRoom.type,"课题类型");
                }
            },
            // {field : "liveparenttype", title : "父类型", width : "100px"},
            // {field : "livetype", title : "直播类型", width : "100px"},
            {field : "begintime", title : "开始时间", width : "120px"},
            {field : "moneytype", title : "费用类型", width : "90px",/*，0为免费，1为付费，2为加密*/
                formatter: function (value, row, index) {
                    return Utils.commonFmt(value,row,index,conts.classRoom.moneyType,"费用类型");
                }
            },
			// {field : "openid", title : "课程拥有者ID", width : "100px"},
			// {field : "content", title : "课题内容", width : "100px"},
			// {field : "classroomid", title : "课题ID", width : "100px"},
			// {field : "password", title : "加密课密码", width : "100px"},
			{field : "price", title : "费用(￥)", width : "70px"},
			{field : "active", title : "上架状态", width : "90px",/*1上架，0下架*/
                formatter: function (value, row, index) {
                    return Utils.commonFmt(value,row,index,conts.classRoom.active,"上架状态");
                }
            },
			// {field : "goldlist", title : "打赏金币列表", width : "100px"},
			{field : "status", title : "状态", width : "80px",/*: 1为正常,0为课程结束*/
                formatter: function (value, row, index) {
                    return Utils.commonFmt(value,row,index,conts.classRoom.status,"状态");
                }
            },
            {field : "1", title : "操作", width : "160px",formatter:FormatterJS.moreOperate},
			// {field : "guest", title : "嘉宾ID串", width : "100px"},
			// {field : "pptpath", title : "PPT路径", width : "100px"},

			// {field : "hascomment", title : "是否开启评论", width : "120px",
             //    formatter: function (value, row, index) {
             //        return Utils.commonFmt(value,row,index,conts.yesNo,"是否开启评论");
             //    }
            // },
			// {field : "hastip", title : "是否支持打赏", width : "120px",
             //    formatter: function (value, row, index) {
             //        return Utils.commonFmt(value,row,index,conts.yesNo,"是否支持打赏");
             //    }
            // },

			// {field : "imageurl", title : "封面图片", width : "100px"},
		]
	})
}

var vm = new Vue({
	el:'#dpLTE',
	data: {
		keyword: {
		    key:null
        },
        btnVal:null
	},
    watch: {
        "btnVal": function (val, oldval) {
            if (val !== oldval) {
                this.btnAction(val);
            }
        },
    },
	methods : {
		load: function() {
			$('#dataGrid').bootstrapTable('refresh');
		},
		save: function() {
			dialogOpen({
				title: '新增课题',
				url: 'srblive_html/classroom/add.html?_' + $.now(),
                width: '80%',
                height: '80%',
				yes : function(iframeId) {
					top.frames[iframeId].vm.acceptClick();
				},
			});
		},
		edit: function(id) {
            var flag = false;
            if (id!=0) {
                flag = true;
            } else {
                var ck = $('#dataGrid').bootstrapTable('getSelections');
                if (checkedRow(ck)) {
                    flag = true;
                }
                id = ck[0].id;
            }
            if (flag) {
                dialogOpen({
                    title: '编辑课题',
                    url: 'srblive_html/classroom/edit.html?_' + $.now(),
                    width: '80%',
                    height: '80%',
                    success: function(iframeId){
                        top.frames[iframeId].vm.srbliveClassroom.id = id;
                        top.frames[iframeId].vm.setForm();
                    },
                    yes: function(iframeId){
                        top.frames[iframeId].vm.acceptClick();
                    }
                });
            }
		},
		remove: function(id) {
            var ids = []
            var flag = false;
            if (id!=0) {
                flag = true;
                ids[0] = id;
            } else {
                var ck = $('#dataGrid').bootstrapTable('getSelections');
                if (checkedRow(ck)) {
                    flag = true;
                }
                if (checkedArray(ck)) {
                    $.each(ck, function (idx, item) {
                        ids[idx] = item.id;
                    });
                }
            }
            if (flag) {
                $.RemoveForm({
                    url: '../../srblive/classroom/remove?_' + $.now(),
                    param: ids,
                    success: function(data) {
                        vm.load();
                    }
                });
            }
		},
        ppt:function(classRoomId){//PPT管理
            // Utils.uploadDlg(id);
            Utils.openFileGridList(classRoomId);
        },
        endLive:function (id) {//结束直播
            // var ck = $('#dataGrid').bootstrapTable('getSelections');
            // if(checkedRow(ck)){
            //     if(ck[0].status==0){
            //         layer.msg("直播已经结束!");
            //         return false;
            //     }
                $.ConfirmFormEx({
                    msg:"确认结束直播么？",
                    url: '../../srblive/classroom/end?_' + $.now(),
                    param:{id:id},
                    success: function(data) {
                        vm.load();
                    }
                });
            // }
        },
        setActive:function(active,id){//上架、下架
            // var ck = $('#dataGrid').bootstrapTable('getSelections');
            // if(checkedRow(ck)){
            //     if(ck[0].active==active){
            //         layer.msg("已经"+(active==0?"下架":"上架")+"!");
            //         return false;
            //     }
                $.ConfirmFormEx({
                    msg:"确认" + (active==0?"下架":"上架") + '该直播么？',
                    url: '../../srblive/classroom/active?_' + $.now(),
                    param: {
                        id:id,
                        active:active
                    },
                    success: function(data) {
                        vm.load();
                    }
                });
            // }
        },
        // {name:'编辑',val:'edit'},
        // {name:'上架',val:'shangjia'},
        // {name:'下架',val:'xiajia'},
        // {name:'结束直播',val:'end'},
        // {name:'删除',val:'delete'},
        // {name:'PPT管理',val:'ppt'},
        // btnAction:function(_this,code){
        //     console.info(code);
        //     switch(code){
        //         case 'edit':
        //             break;
        //         case 'shangjia':
        //             break;
        //         case 'xiajia':
        //             break;
        //         case 'end':
        //             break;
        //         case 'delete':
        //             break;
        //         case 'ppt':
        //             break;
        //         default:break;
        //     }
        // }
	}
})